from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin
from rest_framework.response import Response

from books.models import HeroInfo
from heros.serialziers import HeroSerializer


class HerosView(GenericAPIView, ListModelMixin, CreateModelMixin):
    # 指定查询集属性 查询对应的模型类数据
    queryset = HeroInfo.objects.all()
    # 指定序列化器
    serializer_class = HeroSerializer

    def get(self, request):
        """
            获取所有英雄
        :param request:
        :return:
        """
        return self.list(request)

    def post(self, request):
        """
            保存英雄
        :param request:
        :return:
        """
        # 返回结果
        return self.create(request)

class HeroView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    # 指定查询集属性 查询对应的模型类数据
    queryset = HeroInfo.objects.all()
    # 指定序列化器
    serializer_class = HeroSerializer

    def get(self, request, pk):

        return self.retrieve(request)

    def put(self, request, pk):

        return self.update(request)

    def delete(self, request, pk):
        # 获取单一数据对象 self.get_object()可以根据pk对应的id值自动从queryset指定的查询集获取到对应id的数据队形
        hero = self.get_object()

        hero.is_delete = True
        hero.save()
        return Response({})
